﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Collections;
using System.Threading;
using System.Text.RegularExpressions;

namespace ServerApp
{
    public class UDPServices
    {
        #region khoi tao 
        UdpClient sendingClient;
        #endregion

        String testIP;
        public void server_Load()
        {
            Thread listenThread = new Thread(new ThreadStart(tets));
            listenThread.Start();
            listenThread.IsBackground = true;

        }
        public String returnIP()
        {
            return testIP;
        }
        public string GetLocalIP()
        {
            string _IP = null;

            // Resolves a host name or IP address to an IPHostEntry instance.
            // IPHostEntry - Provides a container class for Internet host address information.
            System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());

            // IPAddress class contains the address of a computer on an IP network.
            foreach (System.Net.IPAddress _IPAddress in _IPHostEntry.AddressList)
            {
                // InterNetwork indicates that an IP version 4 address is expected
                // when a Socket connects to an endpoint
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    _IP = _IPAddress.ToString();
                }
            }
            return _IP;
        }
        private ControlPowerPoint control = new ControlPowerPoint();
        public void tets()
        {
            String filename = "";
            string[] a = null;	
            int kq = 0;
            int recv;
            int port = 8282;
            byte[] data = new byte[1400];
            string host = "";
            IPEndPoint ipendp = new IPEndPoint(IPAddress.Any, port);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            newsock.Bind(ipendp);
            //   MessageBox.Show("waiting for connection....");
            // IPEndPoint sender = new IPEndPoint(IPAddress.Any, 8080);
            EndPoint tmpRemote = (EndPoint)(ipendp);
            TCPservices tcpSv = new TCPservices();
            bool kt = false;
            host = GetLocalIP();
            while (!kt)
            {
                
                data = new byte[1400];
               recv = newsock.ReceiveFrom(data,ref tmpRemote);
               string test = Encoding.ASCII.GetString(data);
             //  MessageBox.Show("data in while: " + test);
               testIP = test;
               a = Regex.Split(test, "\0");
               if (a[0] != "Open" && a[0] != "Next" && a[0] != "Previous" && a[0] != "Home" && a[0] != "End"&& a[0] != "SendFileName")
               {
                   tcpSv.respones(a[0]);
                  // host = a[0].ToString();
                        

               }
               else
               {

                   string ThongTin = a[0];
                   if (ThongTin.Equals("Open"))
                   {
                       control.OpenPowerPoint(filename);
                   }
                   if (ThongTin.Equals("Next"))
                   {
                       control.Next();
                   }
                   if (ThongTin.Equals("Previous"))
                   {
                       control.Previous();
                   }
                   if (ThongTin.Equals("Home"))
                   {
                       control.Home();
                   }
                   if (ThongTin.Equals("End"))
                   {
                       control.End();
                   }
                   if (ThongTin.Equals("Disconect"))
                   {
                       control.Close();
                       kt = true;
                   }
                   if (ThongTin.Equals("SendFileName"))
                   {
                     
                       SendFileTCP send = new SendFileTCP();
                       filename = send.ReceiveName(host);
                       int ktt = send.ReceiveContentfileTCP(host,filename);
                       if (ktt == 1)
                       {
                        //   MessageBox.Show("Create file successful");
                       }
                       else
                       {
                          // MessageBox.Show("Create file unsuccessful");
                       }
                      

                   }
               }
              
              
            }
            newsock.Close();
            
        }
        

        
    }
}
